<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/21
 * Time: 15:08
 */
namespace  app\admin\controller;

use app\admin\model\Outs;
use app\common\lib\redis\Predis;
use app\common\lib\Util;
use think\Db;

class Live
{
    public function push()
    {
        $type = intval($_POST['type']);
        $team_id = intval($_POST['team_id']);
        $content = trim($_POST['content']);
        $image = isset($_POST['image']) ? trim($_POST['image']) : "";
        //$image = str_replace(config('live.host'), '', $image);
        /*
        $data = [
            'game_id' => 1,
            'team_id' => $team_id,
            'content' => $content,
            'image' => $image,
            'type' => $type,
            'status' => 0,
            'create_time' => time()
        ];
        //写入数据库

        $model = new Outs();
        var_dump($model);
        $rs = $model->save($data);

        if($rs){
            return Util::show(config('code.success'), 'add ok');
        }else{
            return Util::show(config('code.error'), 'add error');
        }*/
        $teams = [
            1 => [
                'name' => '马刺',
                'logo' => '/live/imgs/team1.png'
            ],
            4 => [
                'name' => '火箭',
                'logo' => '/live/imgs/team2.png'
            ]
        ];
        $data = [
            'type' => $type,
            'title' => !empty($teams[$team_id]['name']) ? $teams[$team_id]['name'] : "直播员",
            'logo' =>  !empty($teams[$team_id]['logo']) ? $teams[$team_id]['logo'] : "",
            'content' => !empty($content) ? $content : "",
            'image' => !empty($image) ? $image : "",
        ];

        $task_data = [
            'method' => 'pushLive',
            'data' => $data
        ];
        $_POST['http_server']->task($task_data);

        return Util::show(config('code.success'), 'push ok,see', $data);

        /*
        foreach($_POST['http_server']->connections as $fd)
        {
            $_POST['http_server']->push($fd, json_encode($data));
        }

        //echo "当前服务器共有 ".count($_POST['http_server']->connections). " 个连接\n";
        */
    }
}